<?php
namespace app\interfaces\controller;

use app\interfaces\model\MemberBrowse as MemberBrowseModel;
use think\Db;
use think\Request;
use think\Exception;

class MemberBrowse
{

    /*
     * 用户端 - 浏览记录列表
     * @param  string $user_id      会员ID
     */
    public function browse_list()
    {
        // 获取参数
        $user_id = Request::instance()->param('user_id');

        if (empty($user_id)) throw new Exception('not Empty');
        // 展示数据

        $result = Db::view('member_browse', 'id,member_id,goods_id,create_time')
            ->view('shop_goods', 'title,price,thumb_img,rebate_percentage', 'shop_goods.id = member_browse.goods_id')
            ->where(['member_id' => $user_id])
            ->order(['member_browse.id' => 'desc'])
            ->limit(100)
            ->select()->toArray();
        return ['code' => 200, 'message' => '成功', 'data' => arrayGrouping($result, 'create_time', 'date', 'list')];

    }

    /*
     * 用户端 - 清空浏览记录列表
     * @param  string $id 唯一标示
     */
    public function browse_del($id)
    {

        if (empty($id)) throw new Exception('not Empty');

        $result = MemberBrowseModel::destroy($id);

        if ($result) {
            return ['code' => 200, 'message' => '成功'];
        }

        return ['code' => 401, 'message' => '网络繁忙'];
    }

}
